﻿using Gear.Web.Api.DynamicRouter;
using Gear.Web.Api.DynamicCoding;
using Microsoft.AspNetCore.Mvc;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Gear.Web.Tests.DynamicApi
{
    /// <summary>
    /// 动态编译注册（示例）
    /// </summary>
    [ApiDescriptionSettings("DynamicRoute")]
    public class DynamicCoding: IDynamicRoute
    {
        /// <summary>
        /// 动态编译代码注册
        /// </summary>
        /// <param name="code"></param>
        /// <param name="actionProvider"></param>
        /// <param name="tokenProvider"></param>
        /// <returns></returns>
        [HttpPost]
        public string CompiledRegister([FromForm] string code,
            [FromServices] DynamicActionProvider actionProvider,
            [FromServices] DynamicChangeTokenProvider tokenProvider)
        {
            try
            {
                actionProvider.AddControllers(code);
                tokenProvider.NotifyChanges();
                return "OK";
            }
            catch (Exception ex)
            {
                return ex.Message;
            }
        }
    }
}
